//
// Created by sqp on 2021/11/15.
//
#include "util.h"
#include <QFileInfo>
#include <QDir>
#include <sys/stat.h>

namespace common
{
QString getUserName()
{
    QStringList envVariables;
    envVariables << "USERNAME.*"
                 << "USER.*"
                 << "USERDOMAIN.*"
                 << "HOSTNAME.*"
                 << "DOMAINNAME.*";
    QStringList environment = QProcess::systemEnvironment();
    for (QString string : envVariables) {
        int index = environment.indexOf(QRegExp(string));
        if (index != -1) {
            QStringList stringList = environment.at(index).split('=');
            if (stringList.size() == 2) {
                return stringList.at(1);
            }
        }
    }
    return "";
}

QString getHostName()
{
    char hostName[100] = {};
    gethostname(hostName, sizeof(hostName));
    return hostName;
}

bool isFileExist(QString fileName)
{
    QFileInfo fileInfo(fileName);
    if (fileInfo.exists()) {
        return true;
    } else {
        QDir *dir = new QDir;
        bool ok = dir->mkdir(fileName);
        delete dir;
        return ok;
    }
}

unsigned long getFileSize(const char *filename)
{
    struct stat statFile;
    stat(filename, &statFile);
    int size = statFile.st_size;
    return size;
}

} // namespace common
